package com.iotechn.unimall.data.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.iotechn.unimall.data.domain.DistributionDO;
import com.iotechn.unimall.data.dto.DistributionDTO;
import com.iotechn.unimall.data.dto.DistributionRankingDTO;
import com.iotechn.unimall.data.dto.DistributionUserDTO;
import com.iotechn.unimall.data.dto.UserDTO;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2019/11/27
 * Time: 11:07
 */
public interface DistributionMapper extends BaseMapper<DistributionDO> {

    /**
     * 增加冻结余额
     *
     * @param distributionId
     * @param delta
     * @return
     */
    Integer incFreezeBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta);

    /**
     * 冻结余额 （将余额变为冻结余额） 作废
     *
     * @param distributionId
     * @param delta
     * @return
     */
    Integer freezeBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta);

    /**
     * 将冻结余额变为余额
     *
     * @param distributionId
     * @param delta
     * @return
     */
    Integer unfreezeBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta);

    Integer decBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta,@Param("realDeductionSum") Integer realDeductionSum);

    Integer incBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta,@Param("realDeductionSum") Integer realDeductionSum);


    /**
     * 增加邀请订单数量
     *
     * @param distributionId
     * @return
     */
    Integer incOrderCount(@Param("distributionId") Long distributionId);

    /**
     * 增加邀请人数量
     *
     * @param distributionId
     * @return
     */
    Integer incUserCount(@Param("distributionId") Long distributionId);


    Integer decFreezeBalance(@Param("distributionId") Long distributionId, @Param("delta") Integer delta);

    DistributionDO selectByIdForUpdate(Long id);

    DistributionDO selectByUserIdForUpdate(Long id);

    Integer selectBalanceByUserId(Long userId);


    List<DistributionRankingDTO> getDistributionRanking(@Param("offset") Integer offset, @Param("limit") Integer limit);

    List<DistributionUserDTO> getDistributionUser(@Param("distributionId") Long distributionId);

    List<DistributionUserDTO> getInviteUserOrderCount(@Param("inviteId") Long inviteId);


    List<DistributionDTO> getDistributionUserList(
            @Param("userId") Long userId, @Param("nickname") String nickname, @Param("phone") String phone,
            @Param("status") Integer status, @Param("offset") Integer offset,
            @Param("limit") Integer limit);

    Integer countDistributionUserList(
            @Param("userId") Long userId, @Param("nickname") String nickname, @Param("phone") String phone,
            @Param("status") Integer status);

    List<DistributionDTO> getDistributionUserListDesc(
            @Param("userId") Long userId, @Param("nickname") String nickname, @Param("phone") String phone,
            @Param("status") Integer status,@Param("cardNo") String cardNo, @Param("offset") Integer offset,
            @Param("limit") Integer limit);

    Integer countDistributionUserListDesc(
            @Param("userId") Long userId, @Param("nickname") String nickname, @Param("phone") String phone,
            @Param("status") Integer status,@Param("cardNo") String cardNo);

    /**
     * 查询某个用户的下线情况
     *
     * @param inviteId
     * @param offset
     * @param limit
     * @return
     */
    List<DistributionDTO> getDistributionUserListByInviteId(
            @Param("inviteId") Long inviteId, @Param("offset") Integer offset,
            @Param("limit") Integer limit);

    Integer countDistributionUserListByInviteId(
            @Param("inviteId") Long inviteId);

    DistributionDO selectDistributionById(Long userId);

    //查询总余额
    Integer selectBalanceSum();


    //导出用户卡使用
    List<com.iotechn.unimall.data.dto.excel.DistributionBalanceDetailDTO> listDistributionUserListDesc(
            @Param("cardNo") String cardNo,
            @Param("username") String username, @Param("phone") String phone, @Param("type") Integer type);

    //汇总用户
    List<com.iotechn.unimall.data.dto.excel.DistributionDTO> listDistributionUser(
            @Param("cardNo") String cardNo,
            @Param("username") String username, @Param("phone") String phone);

    /**
     * 会员总余额
     * @param gmtMonth
     * @return
     */
    Integer balanceSum(@Param("gmtMonth") Date gmtMonth);

    /**
     *  会员总数
     * @param gmtMonth
     * @return
     */
    Integer memberUserCount(@Param("gmtMonth") Date gmtMonth);

    /** 门店查询账户信息 */
    List<UserDTO> storeDistributionInfo(@Param("search") String search,@Param("page") Integer page,@Param("limit") Integer limit);
}
